package com.ball.dev.log;


import com.ball.common.utils.UserContext;
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Aspect
@Component
@Slf4j
public class LogParam {

    @Pointcut("execution(* com.ball.dev.controller..*.*(..))")
    public void log() {
    }

    @Before("execution(* com.ball.dev.controller..*.*(..))")
//    @Before("log()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String url = request.getRequestURL().toString();
        String userId = UserContext.getUser();
        String ip = request.getRemoteAddr();
        System.out.println("url:" + url + ",userId:" + userId + ",ip:" + ip);
//        Object[] args = joinPoint.getArgs();
//        Stream<?> stream = ArrayUtils.isEmpty(args) ? Stream.empty() : Arrays.stream(args);
//        List<Object> logArgs = stream
//                .filter(arg -> (!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse)))
//                .collect(Collectors.toList());

        //DOTO  将数据存入数据库
//        String id = "这个id是存入数据数据库的主键id";
//        request.setAttribute("systemId",id);
    }
}

